package GoodsPackage;

import CommentPackage.Comment;
import Util.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/detail.do")
public class GoodsDetailsServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String id = request.getParameter("id");

            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");

            // 查询商品
            String sqlGoods = "SELECT * FROM goods WHERE goods_id=?";
            // 查询与商品相关的评论
            String sqlComments = "SELECT * FROM comments WHERE goods_id=?";

            Connection connection = DBUtil.getConnection();
            PreparedStatement stmtGoods = connection.prepareStatement(sqlGoods);
            stmtGoods.setString(1, id);
            ResultSet rsGoods = stmtGoods.executeQuery();
            Good good = new Good();

            while (rsGoods.next()) {
                good.setGoods_id(rsGoods.getString("goods_id"));
                good.setGoods_name(rsGoods.getString("goods_name"));
                good.setGoods_category(rsGoods.getString("category"));
                good.setUnitprice(rsGoods.getString("unitprice"));
                good.setDetails(rsGoods.getString("details"));
                good.setPic(rsGoods.getString("photo"));
            }

            List<Comment> comments = new ArrayList<>();
            PreparedStatement stmtComments = connection.prepareStatement(sqlComments);
            stmtComments.setString(1, id);
            ResultSet rsComments = stmtComments.executeQuery();

            while (rsComments.next()) {
                Comment comment = new Comment();
                comment.setCommentId(rsComments.getString("comment_id"));
                comment.setGoodsId(rsComments.getString("goods_id"));
                comment.setUser_id(rsComments.getString("user_id"));
                comment.setContent(rsComments.getString("content"));
                comment.setRating(rsComments.getInt("rating"));
                comment.setComment_time(rsComments.getString("comment_time")); // Assuming comment_time is stored as a timestamp
                // Add the comment to the list
                comments.add(comment);
            }

            connection.close();

            request.setAttribute("good", good);
            request.setAttribute("comments", comments);
            request.getRequestDispatcher("details.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
